package com.warmlight.voicepacket.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.warmlight.voicepacket.CommentDetailAcitivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.PostDetalData;
import com.warmlight.voicepacket.ui.communitylm.widget.ReportDialog;
import com.warmlight.voicepacket.widget.ReportCopaPopWindow;

import java.util.ArrayList;
import java.util.List;

import static com.warmlight.voicepacket.utils.UIUtils.getResources;

public class PostDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public interface OnZanDataListener {
        void zanData(String id, int Postion, boolean iszan);
    }

    public OnZanDataListener onZanDataListener;

    public void setOnZanDataListener(OnZanDataListener listener) {
        onZanDataListener = listener;
    }

    public OnClickListener onClickListener;
    public  interface OnClickListener{
        void onClick(int position);
    }
    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }

    private List<PostDetalData.ReplyListBean> list;
    private PostDetalData.PostInfoBean bean;
    private Context context;
    private final RequestOptions options;

    /**
     * @param list    评论列表
     * @param bean    评论头部内容参数
     * @param context 上下文
     */
    public PostDetailsAdapter(List<PostDetalData.ReplyListBean> list, PostDetalData.PostInfoBean bean, Context context) {
        this.list = list;
        this.context = context;
        this.bean = bean;
        options = new RequestOptions();
        options.dontAnimate();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 0) {  //根据不同的viewtype,加载不同的布局
            view = LayoutInflater.from(context).inflate(R.layout.header_postdetalvideo, parent, false);
//            view = LayoutInflater.from(context).inflate(R.layout.post_detail_head,parent,false);

            return new PostDetailsAdapter.ViewHolderHeader(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
            return new PostDetailsAdapter.ViewHolders(view);
        }


    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {  //根据你的条件，返回不同的type
            return 0;
        } else {
            return 1;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == 0) {
//            RelativeLayout itemView = (RelativeLayout) holder.itemView;//父布局
//            if (itemView.getChildCount() == 0) {
//                View view = LayoutInflater.from(context).inflate(R.layout.header_postdetalvideo, itemView, true);
                final ViewHolderHeader viewHolderHeader = (ViewHolderHeader) holder;

            viewHolderHeader.numSharenum.setText(String.valueOf(bean.getShare_num()));
            viewHolderHeader.tvCommunityContent.setText(String.valueOf(bean.getContent()));
            viewHolderHeader.tvLikenum.setText(String.valueOf(bean.getLike_num()));
            viewHolderHeader.tvReplaynum.setText(String.valueOf(bean.getComment_num()));
            viewHolderHeader.tvAuthorNick.setText(bean.getUser().getNickname());
                String category = bean.getCategory();
                if (bean.getIs_like()==1){
                    viewHolderHeader.tvLikenum.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(
                        R.drawable.icon_community_zan_true),null,null,null);
                }else {
                    viewHolderHeader.tvLikenum.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(
                            R.drawable.icon_community_zan_false),null,null,null);
                }

            viewHolderHeader.tvLikenum.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onZanDataListener.zanData(bean.getId(), position, bean.getIs_like()==1);
                    }
                });
                if (options != null) {
                    Glide.with(context).asBitmap().load(bean.getUser().getAvator()).apply(options).into(viewHolderHeader.ivIconAuthor);
                }
                if (TextUtils.equals("1",category)){//图片的
//                    View view =
                    viewHolderHeader.rlVideoLayout.setVisibility(View.GONE);
                    ArrayList<ImageInfo> imageInfo = new ArrayList<>();
                    for (int i = 0; i < bean.getPics().size(); i++) {
                        ImageInfo info = new ImageInfo();
                        info.setThumbnailUrl(bean.getPics().get(i).getU());
                        info.setBigImageUrl(bean.getPics().get(i).getU());
                        imageInfo.add(info);
                    }
                    viewHolderHeader.nineGrid.setAdapter(new NineGridViewClickAdapter(context, imageInfo));
                }else if (TextUtils.equals("2",category)){//视频



                }

//            }else{
//
//            }
        } else {//列表的数据
            final ViewHolders holderOnes = (ViewHolders) holder;
            final PostDetalData.ReplyListBean bean = list.get(position);
            holderOnes.iv_zan.setOnClickListener(new View.OnClickListener() {//点赞数 点赞 加减赞
                @Override
                public void onClick(View view) {
                    onZanDataListener.zanData(bean.getId(), position, bean.isIs_like()==1);
                }
            });
            if (bean.isIs_like()==1) {//设置点赞的状态
                holderOnes.iv_zan.setImageDrawable(getResources().getDrawable(R.drawable.icon_community_zan_true));
            } else {
                holderOnes.iv_zan.setImageDrawable(getResources().getDrawable(R.drawable.icon_community_zan_false));
            }
            holderOnes.tv_time.setText(bean.getDate());
            holderOnes.tv_contentcomment.setText(bean.getContent());
            holderOnes.tv_authortitle.setText(bean.getUser().getNickname());
            holderOnes.tv_zannum.setText(String.valueOf(bean.getLike_num()));
            holderOnes.tv_replynum.setText(bean.getReply_num()+"条回复");
            holderOnes.rl_comment.setOnClickListener(new View.OnClickListener() {//跳转到评论详情
                @Override
                public void onClick(View view) {
                    onClickListener.onClick(position);
                    Intent intent=new Intent(context, CommentDetailAcitivity.class);
                    intent.putExtra("post_id",bean.getPost_id());
                    intent.putExtra("reply_id",bean.getId());
                    intent.putExtra("uid",bean.getUser().getUid());
                    context.startActivity(intent);
                }
            });
            holderOnes.rl_comment.setOnLongClickListener(new View.OnLongClickListener() {//TODO  长按弹起举报弹框
                @Override
                public boolean onLongClick(View view) {
                    ReportCopaPopWindow popWindow=new ReportCopaPopWindow(context);
                    popWindow.setID(bean.getId(),bean.getContent());
                    popWindow.showPopupWindow(holderOnes.iv_zan);
                    return false;
                }
            });
            if (bean.getReply_num().equals("0")) {
                holderOnes.iv_comententry.setVisibility(View.GONE);
                holderOnes.tv_replynum.setVisibility(View.GONE);
            }
            if (TextUtils.equals("0", bean.getIs_god())) {
                holderOnes.iv_glodcomment.setVisibility(View.GONE);
            }
            if (options != null) {
                Glide.with(context).asBitmap().load(bean.getUser().getAvator()).apply(options).into(holderOnes.iv_authoeriv);
            }

            ArrayList<ImageInfo> imageInfo = new ArrayList<>();
            for (int i = 0; i < bean.getPics().size(); i++) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(bean.getPics().get(i).getU());
                info.setBigImageUrl(bean.getPics().get(i).getU());
                imageInfo.add(info);
            }
            holderOnes.nineGrid.setSingleImageSize(400);
            holderOnes.nineGrid.setAdapter(new NineGridViewClickAdapter(context, imageInfo));
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolders extends RecyclerView.ViewHolder {
        NineGridView nineGrid;
        TextView tv_authortitle, tv_time, tv_contentcomment, tv_zannum, tv_replynum;
        LinearLayout ll_commentback;
        ImageView iv_authoeriv;
        ImageView iv_glodcomment;
        ImageView iv_comententry;
        ImageView iv_zan;
        RelativeLayout rl_comment;


        ViewHolders(View itemView) {
            super(itemView);
            nineGrid = itemView.findViewById(R.id.nineGrid);
            tv_authortitle = itemView.findViewById(R.id.tv_authortitle);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_glodcomment = itemView.findViewById(R.id.iv_glodcomment);
            tv_contentcomment = itemView.findViewById(R.id.tv_contentcomment);
            iv_authoeriv = itemView.findViewById(R.id.iv_authoeriv);
            tv_zannum = itemView.findViewById(R.id.tv_zannum);
            ll_commentback = itemView.findViewById(R.id.ll_commentback);
            iv_zan = itemView.findViewById(R.id.iv_zan);
            tv_replynum = itemView.findViewById(R.id.tv_replynum);
            iv_comententry = itemView.findViewById(R.id.iv_comententry);
            rl_comment = itemView.findViewById(R.id.rl_comment);

        }
    }

    class ViewHolderHeader extends RecyclerView.ViewHolder {
        RelativeLayout rlVideoLayout;
        RelativeLayout rlVideoContainer;
        CircularImageView ivIconAuthor;
        TextView tvAuthorNick;
        TextView tvCommunityContent;
        NineGridView nineGrid;
        TextView tvLikenum;
        TextView tvReplaynum;
        TextView numSharenum;


        ViewHolderHeader(View itemView) {
            super(itemView);
            rlVideoLayout =  itemView.findViewById(R.id.rl_video_layout);
            rlVideoContainer = itemView.findViewById(R.id.rl_video_container);
            ivIconAuthor = itemView.findViewById(R.id.iv_icon_author);
            tvAuthorNick =  itemView.findViewById(R.id.tv_author_nick);
            tvCommunityContent =  itemView.findViewById(R.id.tv_community_content);
            nineGrid =  itemView.findViewById(R.id.nineGrid);
            tvLikenum =  itemView.findViewById(R.id.tv_likenum);
            tvReplaynum =  itemView.findViewById(R.id.tv_replaynum);
            numSharenum =  itemView.findViewById(R.id.num_sharenum);
        }
    }
}
